﻿using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using Direct.IDAL.User.Jurisdiction;

namespace DirectSell.DAL.User.Jurisdiction
{
    /// <summary>
    /// 会员注册仓储类
    /// </summary>
   public  class RegisterRepository :AbsRepository,IRegisterRepository
    {
        #region 初始化数据--郭文昊--2018年8月19日03:57:52

        public RegisterRepository():base("UserInforMations")
        {
           
        }
        #endregion

        #region 获取插入行--郭文昊--2018年8月19日04:15:57
        /// <summary>
        /// 获取插入行
        /// </summary>
        /// <returns></returns>
        protected override string GetInsertFields()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" ([ServiceCentreCode] ")
            .Append(" ,[RefereeCode] ")
            .Append(" ,[UserLevel] ")
            .Append(" ,[UserCode] ")
            .Append(" ,[UserPassWord] ")
            .Append(" ,[UserSecondPassWord] ")
            .Append(" ,[RegistionTime] ")
            .Append(" ,[UserName] ")
            .Append(" ,[IdNumber] ")
            .Append(" ,[Phone] ")
            .Append(" ,[SecurityQuestion] ")
            .Append(" ,[SecretAnswer] ")
            .Append(" ,[BankName] ")
            .Append(" ,[BankCode] ")
            .Append(" ,[CardHolder] ")
            .Append(" ,[LevelName] ")
            .Append(" ,[Area] ")
            .Append(" ,[ContactId] ")
            .Append(" ,[RefereeId] ")
            .Append(" ,[Investment] ")
             .Append(",[ContactCode]) ");
            return sb.ToString();
        }
        #endregion

        #region 获取插入数据
        /// <summary>
        /// 获取插入数据
        /// </summary>
        /// <returns></returns>
        protected override string GetInsertValue()
        {
            return @" (@ServiceCentreCode,@RefereeCode,@UserLevel,@UserCode,@UserPassWord,
                @UserSecondPassWord,@RegistionTime,@UserName,@IdNumber,@Phone,
               @SecurityQuestion,@SecretAnswer,@BankName,@BankCode,@CardHolder,@LevelName,@Area,@ContactId,@RefereeId,@Investment,@ContactCode) ";
        }
        #endregion

        #region 插入用户注册信息--郭文昊--2018年8月19日03:58:21
        /// <summary>
        /// 插入用户注册信息
        /// </summary>
        /// <returns></returns>
        public void InsertRegisterMessage(UserInforMationDetails model,ref Hashtable hsTable)
        {
            base.Insert<UserInforMationDetails>(model, ref hsTable);
        }
        #endregion

        #region 获取级别表信息--郭文昊--2018年11月8日14:56:06
        /// <summary>
        /// 获取级别表信息
        /// </summary>
        /// <returns></returns>
        public  IList<MemberLevel>  QueryMemberLevel()
        {
            StringBuilder str = new StringBuilder();
            str.Append(" SELECT ");
            str.Append(" LevelName, ");
            str.Append(" Amount, ");
            str.Append(" LevelID, ");
            str.Append(" LevelName +'|'+CAST ( Amount AS VARCHAR ) as SelectFileds ");
            str.Append(" FROM ");
            str.Append(" MemberLevel ");
            return  base.Query<MemberLevel>(str.ToString()) as IList<MemberLevel>;
        }
        #endregion

        #region 查询一条级别--郭文昊--2018年11月17日14:53:09
        /// <summary>
        /// 查询一条级别信息
        /// </summary>
        /// <param name="levelId"></param>
        /// <returns></returns>
        public MemberLevel QueryLevel(int levelId)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT * FROM MemberLevel ");
            sb.AppendFormat(" WHERE LevelID={0} ",levelId);
            return base.Query<MemberLevel>(sb.ToString()).FirstOrDefault();
        }
        #endregion

        #region 判断会员编号是否存在
        /// <summary>
        /// 判断会员编号是否存在
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public bool CheckUserCode(string usercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT COUNT(*) FROM UserInforMations WHERE ");
            sb.AppendFormat("  UserCode='{0}' ", usercode);
            return base.QueryFirstT<int>(sb.ToString()) <= 0;
        }
        #endregion

        #region 判断服务中心是否存在--郭文昊--2018年11月17日15:15:04
        /// <summary>
        /// 判断服务中心是否存在
        /// </summary>
        /// <param name="centercode"></param>
        /// <returns></returns>
        public bool CheckServiceCenter(string centercode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT COUNT(*) FROM UserInforMations WHERE ");
            sb.AppendFormat("  UserCode='{0}' AND IsCenterService=1 ",centercode);
            return base.QueryFirstT<int>(sb.ToString()) > 0;
        }
        #endregion

        #region 判断接点人/推荐人是否存在
        /// <summary>
        /// 判断接点人/推荐人
        /// </summary>
        /// <param name="ContactCode"></param>
        /// <returns></returns>
        public bool ChekConcat(string ContactCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT COUNT (*) FROM UserInforMations ");
            sb.AppendFormat(" WHERE UserCode = '{0}' AND UserStatus = 1 ", ContactCode);
            return  base.QueryFirstT<int>(sb.ToString()) >0;
           
        }
        #endregion

        #region 判断点位上是否有会员
        /// <summary>
        /// 判断点位上是否有会员
        /// </summary>
        /// <returns>有返回false 没有返回true</returns>
        public bool CheckPoint(string contactCode,int area)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT COUNT (*) FROM UserInforMations ");
            sb.AppendFormat(" WHERE ContactCode='{0}' AND Area={1} ", contactCode, area);
            var count =   base.QueryFirstT<int>(sb.ToString());
            return count<=0;
        }
        #endregion

        #region 首次推荐在左区
        /// <summary>
        /// 判断首次推荐必须在左区
        /// </summary>
        /// <returns></returns>
        public bool CheckFirstArea(string usercode,int area)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" SELECT count(*) FROM UserInforMations ");
            sb.AppendFormat(" WHERE RefereeCode='{0}' ",usercode);
            var count = base.QueryFirstT<int>(sb.ToString());
            if (count == 0 &&area!=1)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 查询用户信息--郭文昊--2018年10月29日11:12:19
        /// <summary>
        /// 查询该用户信息
        /// </summary>
        /// <param name="UserCode">会员编号</param>
        /// <returns></returns>
        public UserInforMationDetails GetInfo(string UserCode)
        {
            var sql = new StringBuilder().Append(" SELECT *  FROM UserInforMations ")
               .AppendFormat(" WHERE UserCode ='{0}'", UserCode);
            return base.Query<UserInforMationDetails>(sql.ToString()).FirstOrDefault();
        }
        #endregion

    }
}